package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
/**
 * RestController注解:
 *   1.组合注解,等价于 Controller + ResponseBody 注解;
 *   2.该注解会在每个控制器方法上添加@ResponseBody注解;
 * Slf4j注解:
 *   1.Lombok提供的日志注解,添加在类上,会自动生成日志对象log;
 *   2.日志对象log,会自动生成,使用日志对象log,记录日志;
 *     log.trace("xxx");
 *     log.debug("xxx"); -------------使用最多,开发过程中需要打印调试日志;
 *     log.info("xxx");
 *     log.warn("xxx");
 *     log.error("xxx"); -------------经常使用,记录错误日志[在全局异常处理器中使用较多];
 *  Knife4j注解:
 *    1.Tag(name=""): 调整控制器类名在Knife4j文档中的显示名称;
 *    2.Operation(summary = "xxx"): 添加方法名称,在Knife4j文档中显示的方法名称;
 *    3.ApiOperationSupport(order = 10): 方法排序,数字越小越靠前[优先级越高];
 *    4.Schema(description="xxx",required=true,example="xxx"): 封装参数属性的描述,是否必须,示例值;
 */
@Slf4j
@RestController
@Tag(name = "01-咨询管理模块")
@RequestMapping("/v1/notice")
public class NoticeController  {
    //自动装配 在mapper里面加@Mapper
    @Autowired

    private NoticeMapper noticeMapper;

    @PostMapping("/add")
    @Operation(summary = "添加咨询")
    @ApiOperationSupport(order=10)
    @Parameters(value = {
            @Parameter(name= "title" , description = "标题"),
            @Parameter(name= "content" , description = "内容"),
            @Parameter(name= "type" , description = "类型"),
            @Parameter(name= "status" , description = "状态"),
            @Parameter(name = "noticeAddParam",hidden = true)

    })
    public JsonResult add(NoticeAddParam noticeAddParam)
    {
        log.debug("noticeAddParam = " + noticeAddParam);
        //目标：将资讯信息保存在数据库中
        Notice notice = new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        noticeMapper.insertNotice(notice);

        return JsonResult.ok(notice);
    }


    @GetMapping("/list")
    @Operation(summary = "咨询列表")
    @ApiOperationSupport(order=20)
    public JsonResult list(NoticeListQuery noticeListQuery) {
        log.debug("noticeListQuery = " + noticeListQuery);
        List<NoticeListVO> noticeListVOS = noticeMapper.listNotice(noticeListQuery);

        return JsonResult.ok(noticeListVOS);
    }

    /**
     * 3.资讯详情功能
     */
    @GetMapping("/detail")
    @Operation(summary = "咨询详情")
    @ApiOperationSupport(order=30)
    @Parameters(value =  {
            @Parameter(name = "id" , description = "咨询id" , required = true ,example = "1"),
            @Parameter(name = "name" , description = "名称" ,required = true ,hidden = true)
            //如果有多个参数这样子写
    })
    public JsonResult detail(Long id,String name){
        log.debug("id = " + id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.getNoticeDetailInfoById(id);

        return JsonResult.ok(noticeDetailInfoVO);
    }

    /**
     * 4.删除资讯功能
     */
    @DeleteMapping("/del")
    @Operation(summary = "删除咨询")
    @ApiOperationSupport(order=40)
    public JsonResult del(Long id)
    {
        log.debug("id = " + id);
        noticeMapper.deleteNoticeById(id);
        int num = noticeMapper.deleteNoticeById(id);
        if(num > 0){
           return JsonResult.ok();

        }else {
            return new JsonResult(StatusCode.OPERATION_FAIL);
        }
    }


    /**
     *
     *5.修改资讯功能
     *UPDATE notice
     *SET title = ?, content = ?, type = ?, status = ?, update_time = ? WHERE id = ?
     *UPDATE notice
     *
     * @param
     * @return
     */
    @PostMapping("/update")
    @Operation(summary = "更新咨询")
    @ApiOperationSupport(order=50)
    public JsonResult update(@RequestBody NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam = " + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
       int num = noticeMapper.updateNoticeById(notice);
       if(num>0){
           return JsonResult.ok();

       }else{
           return new JsonResult(StatusCode.OPERATION_FAIL);
       }

    }
}

